package com.oriental.manage.controller.specially;

import com.oriental.manage.core.paging.Pagination;
import com.oriental.manage.core.response.ResponseDTO;
import com.oriental.manage.core.utils.StringUtils;
import com.oriental.manage.pojo.specially.ReissueApply;
import com.oriental.manage.service.specially.ReissueApplyService;
import com.oriental.opcif.common.util.StringUtil;
import com.oriental.riskfront.api.enums.TransCode;
import com.oriental.riskfront.api.model.specially.ResponseModel;
import com.oriental.riskfront.api.model.specially.TSendMessageDto;
import com.oriental.riskfront.api.service.TSendMessageInterface;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Author: yutao
 * Date: 2017/5/2
 * Time: 11:12
 * Desc：
 */
@Controller
@Slf4j
@RequestMapping("informationPush/apply")
public class ReissueApplyController {

    @Autowired
    private ReissueApplyService reissueApplyService;

    @Autowired
    private TSendMessageInterface tSendMessageInterface;

    @RequestMapping("init")
    public String init(){
        return "specially/searchReissueApply";
    }

    @RequestMapping("search")
    @ResponseBody
    public ResponseDTO<Pagination<TSendMessageDto>> search(@RequestBody Pagination<TSendMessageDto> pagination){
        ResponseDTO<Pagination<TSendMessageDto>> responseDTO = new ResponseDTO<>();
        try{
            ResponseModel<List<TSendMessageDto>> responseModel = tSendMessageInterface.queryReissueApply(pagination.getQueryBean());
            if(responseModel.isSuccess()){
                pagination.setList(responseModel.getResult());
                pagination.setRowCount(0);
                if (responseModel.getResult().size() > 0) {
                    pagination.setRowCount(responseModel.getResult().get(0).getRowCount());
                }
                responseDTO.setObject(pagination);
                responseDTO.setSuccess(true);
            }else {
                responseDTO.setSuccess(false);
                responseDTO.setMsg("查询异常");
            }

        }catch (Exception e){
            log.error("查询申请补发异常:{}",e);
            responseDTO.setSuccess(false);
            responseDTO.setMsg("查询异常");
        }
        return responseDTO;

    }

    @RequestMapping("toAdd")
    public String toAdd(){
        return "specially/addReissueApply";
    }


    @RequestMapping("add")
    @ResponseBody
    public ResponseDTO<String> add(@RequestBody ReissueApply reissueApply){
        ResponseDTO<String> responseDTO = new ResponseDTO<>();
        try{
            TSendMessageDto queryDto = new TSendMessageDto();
            String info ="";
            if(reissueApply.getRiskType().equals("01")){
                //黑名单
                queryDto.setKind(TransCode.TS0001.getCode());
                info = "黑名单";
            }else{//风险提示名单
                queryDto.setKind(TransCode.TS0002.getCode());
                info = "风险提示名单";
            }
            queryDto.setType("03");//推送
            queryDto.setMessageDate(reissueApply.getReqDate());
            ResponseModel<List<TSendMessageDto>> checkTS00012Model = tSendMessageInterface.queryTSendMessage(queryDto);
            if (null != checkTS00012Model && checkTS00012Model.getResult().size() > 0){
                    //已经推送过黑名单风险提示名单信息
                    responseDTO.setSuccess(false);
                    responseDTO.setMsg("该日已推送过'"+info+"'信息。");
                    return responseDTO;

            }else{
                    //查询当天是补发 且为补发成功的
                    queryDto = new TSendMessageDto();
                    queryDto.setKind(TransCode.TS0004.getCode());
                    queryDto.setMessageDate(reissueApply.getReqDate());
                    queryDto.setType("01");//补发请求响应
                    queryDto.setRiskType(reissueApply.getRiskType());
                    queryDto.setStatus("01");//补发成功
                    ResponseModel<List<TSendMessageDto>> checkTS0004Model = tSendMessageInterface.queryTSendMessage(queryDto);
                    if (null != checkTS0004Model && checkTS0004Model.getResult().size() > 0){
                            responseDTO.setSuccess(false);
                            responseDTO.setMsg("该日申请成功'"+info+"'信息。");
                            return responseDTO;
                    }
            }
            reissueApplyService.addReissueApply(reissueApply,responseDTO);
            responseDTO.setSuccess(true);
        }catch (Exception e){
            log.error("新增申请补发异常:{}",e);
            responseDTO.setSuccess(false);
            responseDTO.setMsg("系统异常");
        }
        return responseDTO;
    }

}
